package bog

class QuestionController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


    def upVote() {
        Question q = Question.get(params.questionId)

        q.upVote(Long.parseLong(params.userId))
        q.save(flush: true)

        redirect(controller: "thread", action: "show", id: q.thread.id)
    }

    def downVote() {
        Question q = Question.get(params.questionId)
        q.downVote(Long.parseLong(params.userId))
        q.save(flush: true)

        redirect(controller: "thread", action: "show", id: q.thread.id)
    }

    def edit(Long id) {
        def questionInstance = Question.get(id)
        if (!questionInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'question.label', default: 'Question'), id])
            redirect(action: "list")
            return
        }

        [questionInstance: questionInstance]
    }

    def update(Long id, Long version) {
        def questionInstance = Question.get(id)
        if (!questionInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'question.label', default: 'Question'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (questionInstance.version > version) {
                questionInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'question.label', default: 'Question')] as Object[],
                        "Another user has updated this Question while you were editing")
                render(view: "edit", model: [questionInstance: questionInstance])
                return
            }
        }

        questionInstance.properties = params

        if (!questionInstance.save(flush: true)) {
            render(view: "edit", model: [questionInstance: questionInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'question.label', default: 'Question'), questionInstance.id])
//        redirect(action: "show", id: questionInstance.id)
        redirect(controller: 'thread', action: 'show', id: questionInstance.thread.id)
    }
//
//    def delete(Long id) {
//        def questionInstance = Question.get(id)
//        if (!questionInstance) {
//            flash.message = message(code: 'default.not.found.message', args: [message(code: 'question.label', default: 'Question'), id])
//            redirect(action: "list")
//            return
//        }
//
//        try {
//            questionInstance.delete(flush: true)
//            flash.message = message(code: 'default.deleted.message', args: [message(code: 'question.label', default: 'Question'), id])
//            redirect(action: "list")
//        }
//        catch (DataIntegrityViolationException e) {
//            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'question.label', default: 'Question'), id])
//            redirect(action: "show", id: id)
//        }
//    }
}
